package com.yedean.mall.controller;

/**
 * UmsAdminController
 *
 * @author yedean
 * @date 2022/8/11
 */

import com.yedean.mall.api.CommonResult;
import com.yedean.mall.entity.UmsAdmin;
import com.yedean.mall.entity.UmsRole;
import com.yedean.mall.service.IUmsAdminService;
import com.yedean.mall.service.IUmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Api(tags = "后台用户管理")
@RequestMapping("/admin/admin")
public class UmsAdminController {


    @Autowired
    private IUmsAdminService adminService;
    @Autowired
    private IUmsRoleService roleService;


    @ApiOperation("获取指定用户信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsAdmin> getItem(@PathVariable Long id) {
        UmsAdmin admin = adminService.getById(id);
        return CommonResult.success(admin);
    }


    @ApiOperation("修改指定用户信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> update(@PathVariable Long id, @RequestBody UmsAdmin admin) {

        admin.setId(id);
        boolean update = adminService.updateById(admin);
        if (update) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除指定用户信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> delete(@PathVariable Long id) {
        boolean remove = adminService.removeById(id);
        if (remove) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @ApiOperation("修改帐号状态")
    @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> updateStatus(@PathVariable Long id, @RequestParam(value = "status") Integer status) {
        UmsAdmin umsAdmin = new UmsAdmin();
        umsAdmin.setStatus(status);
        umsAdmin.setId(id);
        boolean update = adminService.updateById(umsAdmin);
        if (update) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    @ApiOperation("给用户分配角色")
    @RequestMapping(value = "/role/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<?> updateRole(@RequestParam("adminId") Long adminId,
                                   @RequestParam("roleIds") List<Long> roleIds) {
        
        adminService.updateRole(adminId, roleIds);
        return CommonResult.success();
    }

    @ApiOperation("获取指定用户的角色")
    @RequestMapping(value = "/role/{adminId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UmsRole>> getRoleList(@PathVariable Long adminId) {
        List<UmsRole> roleList = adminService.getRoleList(adminId);
        return CommonResult.success(roleList);
    }

}
